﻿using System;
using System.Data;
using System.Xml;
using System.IO;

namespace BTS.COMMON
{
    public class BTSConfiguration
    {
        // Constructors

        // Attributes
        private static string m_strConfigurationFileName;
        private static string m_strConfigurationFilePath;
        private static string m_strConfigurationFile;
        private static XmlDocument xDoc;

        // Methods
        private static void InitSaveXMLFile()
        {
            if (!System.IO.File.Exists(m_strConfigurationFile))
            {
                try
                {
                    xDoc = new XmlDocument();

                    XmlNode newNode;
                    newNode = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                    xDoc.AppendChild(newNode);

                    newNode = xDoc.CreateNode(XmlNodeType.Element, "CONFIGURATIONS", null);
                    xDoc.AppendChild(newNode);

                    Save();
                }
                catch (Exception ex)
                {
                    BTSLog.DoLog("CBTSConfiguration.InitSaveXMLFile", m_strConfigurationFile + "\r\n" + ex.Message, ex.StackTrace);
                }
            }
            else
            {
                xDoc = new XmlDocument();
                xDoc.Load(m_strConfigurationFile);
            }
        }

        public static void Initialize()
        {
            m_strConfigurationFileName = "Configuration.XML";

            m_strConfigurationFilePath = System.AppDomain.CurrentDomain.BaseDirectory;
            m_strConfigurationFilePath += "\\CONFIGURATIONS";
            m_strConfigurationFile = String.Format("{0:S}\\{1:S}", m_strConfigurationFilePath, m_strConfigurationFileName);

            if (!System.IO.Directory.Exists(m_strConfigurationFilePath))
                System.IO.Directory.CreateDirectory(m_strConfigurationFilePath);
            InitSaveXMLFile();
        }

        public static void SetConfigurationFileName(string strConfigurationFileName)
        {
            m_strConfigurationFileName = strConfigurationFileName;
            m_strConfigurationFile = String.Format("{0:S}\\{1:S}", m_strConfigurationFilePath, m_strConfigurationFileName);
        }

        public static bool Save()
        {
            try
            {
                xDoc.Save(m_strConfigurationFile);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static void SetNodeValue(string strNodeName, string strNodeValue)
        {
            xDoc.Load(m_strConfigurationFile);

            XmlNode nodeRoot = xDoc.SelectSingleNode("CONFIGURATIONS");
            if (nodeRoot == null)
                return;

            XmlNode nodeSet = nodeRoot.SelectSingleNode(strNodeName);
            if (nodeSet == null)
            {
                nodeSet = xDoc.CreateNode(XmlNodeType.Element, strNodeName, null);
                nodeSet.InnerText = strNodeValue;
                nodeRoot.AppendChild(nodeSet);
            }
            else
            {
                nodeSet.InnerText = strNodeValue;
            }
        }

        public static void SetNodeValue(string strNodeName, bool bNodeValue)
        {
            string strNodeValue = String.Format("{0:D}", bNodeValue);
            SetNodeValue(strNodeName, strNodeValue);
        }

        public static void SetNodeValue(string strNodeName, int nNodeValue)
        {
            string strNodeValue = String.Format("{0:D}", nNodeValue);
            SetNodeValue(strNodeName, strNodeValue);
        }

        public static void SetNodeValue(string strNodeName, double dblNodeValue)
        {
            string strNodeValue = dblNodeValue.ToString("F2");
            SetNodeValue(strNodeName, strNodeValue);
        }

        public static string GetNodeValueString(string strNodeName)
        {
            try
            {
                Initialize();
                string strNodeValue = "";

                if (!System.IO.File.Exists(m_strConfigurationFile))
                    return strNodeValue;

                XmlNode nodeRoot = xDoc.SelectSingleNode("CONFIGURATIONS");
                if (nodeRoot == null)
                    return strNodeValue;

                XmlNode nodeGet = nodeRoot.SelectSingleNode(strNodeName);
                if (nodeGet == null)
                    return strNodeValue;

                strNodeValue = nodeGet.InnerText;

                return strNodeValue;
            }
            catch (Exception ex)
            {
                BTSLog.DoLog("CBTSConfiguration.GetNodeValueString", m_strConfigurationFile + "\r\n" + strNodeName + "\r\n" + ex.Message, ex.StackTrace);
                return "";
            }

        }

        public static bool GetNodeValueBoolean(string strNodeName)
        {
            string strNodeValue = GetNodeValueString(strNodeName);
            return Convert.ToBoolean(strNodeValue);
        }

        public static int GetNodeValueInt(string strNodeName)
        {
            string strNodeValue = GetNodeValueString(strNodeName);
            return Convert.ToInt16(strNodeValue);
        }

        public static double GetNodeValueDouble(string strNodeName)
        {
            string strNodeValue = GetNodeValueString(strNodeName);
            return Convert.ToDouble(strNodeValue);
        }

        public static void SetNodeTable(DataTable tableValue, string RowName)
        {
            if (tableValue.Rows.Count <= 0 || string.IsNullOrEmpty(RowName))
                return;

            XmlNode nodeRoot = xDoc.SelectSingleNode("CONFIGURATIONS");
            if (nodeRoot == null)
                return;

            XmlNode nodeSet = nodeRoot.SelectSingleNode(tableValue.TableName);
            if (nodeSet == null)
            {
                nodeSet = xDoc.CreateNode(XmlNodeType.Element, tableValue.TableName, null);
                nodeRoot.AppendChild(nodeSet);
            }

            foreach (DataRow ROW in tableValue.Rows)
            {
                XmlNode nodeRow = xDoc.CreateNode(XmlNodeType.Element, RowName, null);
                nodeSet.AppendChild(nodeRow);

                for (int i = 0; i < tableValue.Columns.Count; i++)
                {
                    XmlNode nodeCol = xDoc.CreateNode(XmlNodeType.Element, tableValue.Columns[i].ColumnName.ToString(), null);
                    nodeCol.InnerText = ROW[i].ToString();
                    nodeRow.AppendChild(nodeCol);
                }
                nodeSet.AppendChild(nodeRow);
            }
        }

        public static XmlNodeList GetNodeList(string strNodeName)
        {
            if (!System.IO.File.Exists(m_strConfigurationFile))
                return null;
            strNodeName = "CONFIGURATIONS/" + strNodeName;
            XmlNodeList nodeListGet = xDoc.SelectNodes(strNodeName);
            return nodeListGet;
        }

        public static DataTable GetNodeTable(string strNodeName)
        {
            if (!System.IO.File.Exists(m_strConfigurationFile))
                return null;

            XmlNode nodeRoot = xDoc.SelectSingleNode("CONFIGURATIONS");
            if (nodeRoot == null)
                return null;

            XmlNode nodeGet = nodeRoot.SelectSingleNode(strNodeName);
            if (nodeGet == null)
                return null;

            XmlNodeList nodeListGet = nodeGet.ChildNodes;
            if (nodeListGet.Count <= 0)
                return null;

            DataTable tableReturn = new DataTable(strNodeName);

            XmlNodeList nodeListItem;
            nodeListItem = nodeListGet.Item(0).ChildNodes;

            // Set Columns
            foreach (XmlNode nodeItem in nodeListItem)
            {
                DataColumn dcColumn = new DataColumn(nodeItem.Name, System.Type.GetType("System.String"));
                tableReturn.Columns.Add(dcColumn);
            }

            foreach (XmlNode nodeItemGet in nodeListGet)
            {

                DataRow drRow = tableReturn.NewRow();

                int nCol = 0;
                nodeListItem = nodeItemGet.ChildNodes;
                foreach (XmlNode nodeItem in nodeListItem)
                {
                    drRow[nCol] = nodeItem.InnerText;
                    nCol++;
                }

                tableReturn.Rows.Add(drRow);
            }

            return tableReturn;
        }
    }
}
